//
//  STAlertModel.swift
//  StarTimesON
//
//  Created by Toj on 3/4/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

struct STAlertModel {
    
    /// 后台数据
    // 目的页
    var link = ""
    // 中间图
    var popUrl = ""
    // 头图
    var topPicUrl = ""
    // 标题
    var title = ""
    // 描述信息
    var popDescription = ""
    // 确定
    var popBtn = ""
    // 取消
    var popCancelBtn = ""
    
    // 弹窗名称, 报数用
    var name = ""
    
    /// 自定义数据
    // 默认按钮的标题
    var defalutTitle: String {
        if !popBtn.isEmpty { return popBtn }
        return "ok"
    }
    // 取消按钮的标题
    var cancelTitle: String {
        if !popCancelBtn.isEmpty { return popCancelBtn }
        return "cancel"
    }
    var isCancel = true
    // 点击背景关闭
    var isEffectClose = false
    
    // 字体
    let titleFont: UIFont = UIFont.boldSystemFont(ofSize: 16)
    let descriptionFont: UIFont = UIFont.systemFont(ofSize: 14)
    
    /// 布局
    // 图片高度
    public var imageHeight: CGFloat = 0
    // 标题高度
    private(set) var titleHeight: CGFloat = 0
    // 描述高度
    private(set) var descriptionHeight: CGFloat = 0
    // 按钮高度
    private(set) var buttonHeight: CGFloat = 48
    // 垂直层级 间隙
    public let spacing: CGFloat = 16
    
    // 弹框内容宽度
    private(set) var contentWidth: CGFloat = 0
    // 弹框内容高度
    private(set) var contentHeight: CGFloat = 0
    
    private(set) var logoSpacing: CGFloat = 12
    private(set) var logoWH: CGFloat = 100
    
    /// 外面可改动字段
    // 中间图加载完毕
    var imageLoaded = false
}

extension STAlertModel {
    
    var onlyButton: Bool {
        let hasContent = !title.isEmpty || !popDescription.isEmpty || !topPicUrl.isEmpty || !popUrl.isEmpty
        return !hasContent
    }
    
    var contentNotImage: Bool {
        return topPicUrl.isEmpty && (!popDescription.isEmpty || !title.isEmpty)
    }
    
    mutating func alertHeight(max width: CGFloat) -> CGFloat {
        guard contentHeight == 0 else {
            var result = contentHeight
            if !popUrl.isEmpty && imageLoaded {
                result += (logoWH + 8)
            }
            return result
        }
        
        contentWidth = width
        var result = buttonHeight
        var spacingCount = 1
        if !title.isEmpty {
            titleHeight = title.cm_stringHeight(font: titleFont, maxWidth: width-spacing*2) // 减去左右间距
            result += titleHeight
            spacingCount += 1
        }
        
        if !popDescription.isEmpty {
            descriptionHeight = popDescription.cm_stringHeight(font: descriptionFont, maxWidth: width-spacing*2) // 减去左右间距
            result += descriptionHeight
            spacingCount += 1
        }
        
        result += (CGFloat(spacingCount) * spacing) // 上下间距
        if contentNotImage { result += 8 }
        if onlyButton { result = 56 }
        
        contentHeight = result
        if !popUrl.isEmpty && imageLoaded { result += (logoWH + 8) }
        return result
    }
}

struct STVVActionType {
    var rawValue: String
    
    static let none        = STVVActionType(rawValue:"none")
    static let launch      = STVVActionType(rawValue:"launch")
    static let watch       = STVVActionType(rawValue: "watch")
    static let register    = STVVActionType(rawValue: "register")
    static let login       = STVVActionType(rawValue: "login")
    static let linkcard    = STVVActionType(rawValue:"linkcard")
    static let share       = STVVActionType(rawValue:"share")
    static let tabtap      = STVVActionType(rawValue: "tabtap")
    static let entrancetap = STVVActionType(rawValue: "entrancetap")
    
    enum VVWatchType: String {
        case live
        case vod
    }
    
    enum VVTabtapType: String {
        case home
        case match
        case livetv
        case me
        
       static func indexValue(_ index: Int) -> Self? {
            switch index {
            case 0:
                return .home
            case 1:
                return .match
            case 2:
                return .livetv
            case 3:
                return .me
            default:
                return nil
            }
        }
    }
    
    enum VVEntrancetapType: String {
        case search
        case filter
        case me
    }
}

struct STVVParameter {
    var action: STVVActionType = STVVActionType.none
    
    var category: String? = nil
    
    var id: String? = nil
}

extension STVVParameter {
    var utmParam: String {
        var param = action.rawValue
        guard let _category = category else { return param }
        param += "-" + _category
        
        guard let _id = id else { return param }
        param += "-" + _id
        return param
    }
}
